package org.hitchhike.web.front.controller;

import org.hitchhike.common.web.controller.BaseController;
import org.hitchhike.core.domain.User;
import org.hitchhike.core.enums.Constant;
import org.hitchhike.core.enums.UserStateEnum;
import org.hitchhike.core.exception.ErrorInfo;
import org.hitchhike.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping(value = "/front/user")
public class UserController extends BaseController<User, Long> {

  @Autowired
  private UserService userService;

  /**
   * 车主查询
   */
  @RequestMapping(value = "/driver", method = RequestMethod.GET)
  public String driver(Model model, Integer page) {
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    model.addAttribute("page", userService.findPageDriver(page));
    model.addAttribute("nav_active", "driver");
    return viewName("driver");
  }

  /**
   * 乘客查询
   */
  @RequestMapping(value = "/passenger", method = RequestMethod.GET)
  public String passenger(Model model, Integer page) {
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    model.addAttribute("page", userService.findPagePassenger(page));
    model.addAttribute("nav_active", "passenger");
    return viewName("passenger");
  }


  @RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET)
  public String updateUserState(RedirectAttributes redirectAttributes, @PathVariable String type,
      @PathVariable Long id) {
    ErrorInfo<String> r = new ErrorInfo<>();
    try {
      User user = userService.get(id);
      if (user.getUserType() == Constant.TYPE_1) {
        if (user.getUserState() == UserStateEnum.DISABLED) {
          user.setUserState(UserStateEnum.ENABLED);
        } else {
          user.setUserState(UserStateEnum.DISABLED);
        }
      } else if (user.getUserType() == Constant.TYPE_2) {
        if (user.getUserState() == UserStateEnum.PENDING) {
          user.setUserState(UserStateEnum.FINISHED);
        } else {
          user.setUserState(UserStateEnum.PENDING);
        }
      }
      userService.update(user);

      r.setCode(ErrorInfo.OK);
      r.setMessage(ErrorInfo.MESSAGE_OK);
    } catch (Exception e) {
      r.setCode(ErrorInfo.ERROR);
      r.setMessage(ErrorInfo.MESSAGE_ERROR);
    }
    redirectAttributes.addFlashAttribute("r", r);

    if (type.equals("driver")) {
      return redirectToUrl(viewName("driver"));
    } else {
      return redirectToUrl(viewName("passenger"));
    }
  }

}
